package servlet.huwenyao;

import pojo.Evaluate;
import pojo.Goods;
import pojo.Order;
import pojo.User;
import service.EvaluateService;
import service.OrderService;
import service.impl.EvaluateServiceImpl;
import service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 修改评分回显Servlet页面
 */
@WebServlet(value = "/UpdateEvaluateEchoServlet")
public class UpdateEvaluateEchoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //当前返回的是JSON格式
        response.setContentType("text/json;charset=UTF-8");
        //接收页面的评论id
        String id = request.getParameter("id");
        EvaluateService evaluateService = new EvaluateServiceImpl();
        //根据评论id获取评论对象
        Evaluate evaluate = evaluateService.finaEvaluate(Integer.parseInt(id));
        //通过评论对象接收用户id
        int consumerId = evaluate.getConsumerId();
        OrderService orderService = new OrderServiceImpl();
        //根据用户id获取用户对象
        User user = orderService.finaUser(consumerId);
        //根据用户id获取已完成的订单对象中的商品id集合并进行分割
        String wareId[] = orderService.finalOrder(consumerId).getWareId().split(",");
        List<Goods> goodsList = new ArrayList<Goods>();
        //遍历商品id字符串数组
        if (wareId.length != 0) {
            for (int i = 0; i < wareId.length; i++) {
                //将通过商品查询商品对象并添加到商品集合中
                goodsList.add(orderService.finaGoods(Integer.parseInt(wareId[i])));
            }
            request.setAttribute("user", user);
            request.setAttribute("goodsList", goodsList);
            request.setAttribute("evaluate", evaluate);
            request.getRequestDispatcher("jsp/hwy/UpdateEvaluate.jsp").forward(request, response);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
